#include <cstdio>

int a[12];
bool book[12];
long long cnt;

int N;

int min_count;

struct line {
	int p1;
	int p2;
	line() : p1(0), p2(0) { }
	line(int p1, int p2): p1(p1), p2(p2) { }
};

line L[12];

int W[100] = { 0, //0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //10
	0, 0, 2, 0, 0, 0, 4, 0, 5, 0, //20
	0, 0, 0, 0, 0, 0, 0, 5, 0, 0, //30
	2, 0, 0, 0, 0, 0, 5, 0, 6, 0, //40
	0, 0, 0, 0, 0, 5, 0, 0, 0, 0, //50
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //60
	0, 0, 0, 5, 0, 0, 0, 0, 0, 0, //70
	4, 0, 5, 0, 0, 0, 0, 0, 8, 0, //80
	0, 5, 0, 0, 0, 0, 0, 0, 0, 0, //90
	5, 0, 6, 0, 0, 0, 8, 0, 0
};

bool check(int i, int n) {
	if (n==0) return true;
	int t = W[i*10+n];
	if (t==0) return true;
	if (!book[t]) return false;
	return true;
}

bool check2(int n) {
	for (int i=0; i<N; i++) {
		int p1 = L[i].p1;
		int p2 = L[i].p2;
		bool flag = false;
		for (int j=2; j<n; j++) {
			if (a[j-1]==p1 && a[j]==p2 || a[j-1]==p2 && a[j]==p1) {
				flag = true;
				break;
			}
		}
		if (!flag) return false;
	}
	return true;
}

void dfs(int n) {
	if (n > min_count) {
		if (check2(n))
			cnt++;
	}
	
	if (n > 9) {
		return;
	}
	
	for (int i=1; i<=9; i++) {
		if (book[i]) continue;
		if (!check(i, a[n-1])) continue;
		book[i] = true;
		a[n] = i;
		dfs(n+1);
		book[i] = false;
	}
}

int main() {
	scanf("%d", &N);
	for (int i=0; i<N; i++) {
		int p1, p2;
		scanf("%d%d", &p1, &p2);
		L[i] = line(p1, p2);
	}
	min_count = N>4?N:4;
	dfs(1);
	printf("%lld\n", cnt);
	return 0;
}
